<template>
  <div class="">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('用户名称')" prop="userId">
        <UserSelect v-model="queryParams.userId" size="small" :placeholder="$t('用户名称')" />
      </el-form-item>
      <el-form-item :label="$t('年份')" prop="year">
        <DatePicker
          v-model="queryParams.year"
          :disabled="batchEdit"
          type="year"
          value-format="yyyy"
          format="yyyy"
          :placeholder="$t('年份')"
          :clearable="false"
          @change="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('归属部门')" prop="deptId">
        <tree-select v-model="queryParams.deptId" node-key="id" :default-expand-all="true" :data="deptOptions" :placeholder="$t('请选择')" clearable>
          <label slot="option-label" slot-scope="{ node, shouldShowCount, count, labelClassName, countClassName }" :class="labelClassName">
            {{ $t(node.label) }}
            <span v-if="shouldShowCount" :class="countClassName">({{ count }})</span>
          </label>
        </tree-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          v-hasPermi="['oa:vacationtype:export']"-->
      <!--          type="warning"-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="exportHandle"-->
      <!--        >{{ $t("导出") }}</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['oa:vacationtype:edit']"
          type="warning"
          icon="el-icon-edit"
          size="mini"
          @click="batchEditHandle"
        >{{ $t("批量编辑") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-if="batchEdit"
          size="mini"
          @click="cancel"
        >{{ $t("取消") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-if="batchEdit"
          v-hasPermi="['oa:vacationtype:edit']"
          type="primary"
          size="mini"
          @click="batchSaveHandle"
        >{{ $t("保存") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="oaVacationUserList" height="600" border>
      <el-table-column :label="$t('序号')" type="index" align="center" width="50" />
      <el-table-column :label="$t('用户名称')" align="center" prop="nickName" />
      <el-table-column :label="$t('入职时间')" align="center" prop="entryTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('入职年限')" align="center" prop="employmentYears" />
      <el-table-column :label="$t('开始工作时间')" align="center" prop="startJobTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startJobTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('工作年限')" align="center" prop="jobYears" />
      <el-table-column
        v-for="(item, index) in oaVacationTypeList"
        :key="item.id"
        :label="$t(item.type) + '(' + item.day + ')/' + $t('天')"
        align="center"
        min-width="90"
      >
        <template slot-scope="scope">
          <span v-if="!batchEdit">{{ scope.row.oaVacationTypes[index].day }}</span>
          <el-input-number v-else v-model="scope.row.oaVacationTypes[index].day" :controls="false" :precision="1" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="80">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['oa:vacationtype:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改休假维护用户休假对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <oaVacationUserForm ref="oaVacationUserForm" />
    </el-dialog>
  </div>
</template>

<script>
import { delOaVacationUser, list, batchSaveOaVacationUser } from '@/api/oa/vacation/user/oaVacationUser'
import { listOaVacationType } from '@/api/oa/vacation/type/oaVacationType'
import oaVacationUserForm from './oaVacationUserForm.vue'
import TreeSelect from '@/components/TreeSelect/index'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { treeselect } from '@/api/system/dept'
export default {
    components: {
        oaVacationUserForm,
      TreeSelect
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 休假维护用户休假表格数据
          oaVacationUserList: [],
          oaVacationTypeList: [],
          // 部门树选项
          deptOptions: undefined,
          // 弹出层标题
          title: '休假维护用户休假',
          // 是否显示弹出层
          formOpen: false,
          // 批量编辑
          batchEdit: false,
          // 查询参数
          queryParams: {
              year: new Date().getFullYear() + '',
              userId: undefined,
              deptId: undefined
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
        day: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ] }
      }
  },
  created() {
      this.getList()
      listOaVacationType(null).then(response => {
        this.oaVacationTypeList = response.rows
      })
    this.getTreeselect()
  },
  methods: {
    /** 查询休假维护用户休假列表 */
    getList() {
        this.loading = true
        /* listOaVacationUser(this.queryParams).then(response => {
            this.oaVacationUserList = response.rows
            this.total = response.total
            this.loading = false
        }) */
        if (!this.queryParams.year) {
          this.queryParams.year = new Date().getFullYear() + ''
        }
        list(this.queryParams).then(response => {
          this.oaVacationUserList = response.data
          this.loading = false
        })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            vacationType: undefined,
            userId: undefined,
            day: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.oaVacationUserForm.init(null)
        })
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
      console.log(row)
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            // row.year = this.queryParams.year
            this.$refs.oaVacationUserForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delOaVacationUser(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcel('/user/oaVacationUser/export', this.queryParams)
    },
    /**
     * 批量编辑
     */
    batchEditHandle() {
      this.loading = true
      this.batchEdit = true
      this.loading = false
    },
    /**
     * 批量保存
     */
    batchSaveHandle() {
      this.loading = true
      batchSaveOaVacationUser(this.oaVacationUserList).then(response => {
        if (response.code === 200) {
          this.msgSuccess(this.$t('保存成功'))
          this.closeForm()
        } else {
          this.msgError(response.msg)
        }
        this.loading = false
        this.batchEdit = false
      }).catch(() => {
        this.loading = false
        this.batchEdit = false
      })
    },
    /**
     * 取消
     */
    cancel() {
      this.loading = true
      this.getList()
      this.batchEdit = false
      this.loading = false
    }
  }
}
</script>
